package net.esj.basic.core.engine.support.validator.constraints.validator;

import java.util.Date;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import net.esj.basic.core.engine.support.validator.constraints.CompareDate;

public class CompareDateValidator implements ConstraintValidator<CompareDate, Date[]> {

	DateCompare mode;
	
	@Override
	public void initialize(CompareDate arg0) {
		this.mode = arg0.value();
	}

	@Override
	public boolean isValid(Date[] object, ConstraintValidatorContext arg1) {
		if(object==null){
			return true;
		}
		if(object[0]==null || object[1]==null){
			return true;
		}
		Date first = object[0];
		Date second = object[1];
		switch(mode){
		case AFTER:
			return first.after(second);
		case BEFORE:
			return first.before(second);
		}
		return false;
	}
	
	public static enum DateCompare{
		AFTER,
		BEFORE,
	}

}
